package com;

import tool.jdbctool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ques_find")
public class ques_find extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("find");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<question>list=new ArrayList<>();
        try {
            connection= jdbctool.getconection();
            statement=connection.prepareStatement("" +
                    "select *from `question`");
         resultSet=statement.executeQuery();
         out.write("<table border=''align=\"center\">" +
                 "<tr >" +
                 "<td>问题</td>" +
                 "<td>A</td>" +
                 "<td>B</td>" +
                 "<td>C</td>" +
                 "<td>D</td>" +
                 "<td>答案</td>" +
                 "<td align='center' colspan=\"2\">操作</td>" +
                 "</tr>");
         while (resultSet.next())
         {
             if ("A".equals(resultSet.getString("answer")))
             {

                 out.write("<tr style=\"background: red\"><td>");
             }
             else if ("B".equals(resultSet.getString("answer")))
             {

                 out.write("<tr style=\"background: #00d600\"><td>");
             }
             else if ("C".equals(resultSet.getString("answer")))
             {

                 out.write("<tr style=\"background: #ffff00\"><td>");
             }
             else {

                 out.write("<tr ><td>");
             }
             out.write(resultSet.getString(2)+"</td><td>");
             out.write(resultSet.getString(3)+"</td><td>");
             out.write(resultSet.getString(4)+"</td><td>");
             out.write(resultSet.getString(5)+"</td><td>");
             out.write(resultSet.getString(6)+"</td><td align='center'>");
             out.write(resultSet.getString("answer")+"</td><td align='center'>");
             out.write("<a href='/web4/quesdelete?quesid="+resultSet.getString(1)+"'>删除</a>"+"</td><td>");
             out.write("<a href='/web4/quesupdate?quesid="+resultSet.getString(1)+
                     "&title="+resultSet.getString(2)+
                     "&A="+resultSet.getString(3)+
                     "&B="+resultSet.getString(4)+
                     "&C="+resultSet.getString(5)+
                     "&D="+resultSet.getString(6)+
                     "&answer="+resultSet.getString(7)+
                     "'>详细</a>"+"</td></tr>");
         }
         out.write("</table");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        out.write("查询完毕！！");
    }
}
